//
//  SEGSharedNeihborhoodModel.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/10/18.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGSharedNeihborhoodModel.h"

#if __has_include(<Commom/YYBaseHeader.h>)
#import <Commom/YYBaseHeader.h>
#else
#import "YYBaseHeader.h"
#endif

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<SEGPlatform/SEGBussinessConstHeader.h>)
#import <SEGPlatform/SEGBussinessConstHeader.h>
#else
#import "SEGBussinessConstHeader.h"
#endif


@implementation SEGSharedNeihborhoodModel
- (instancetype)initWithDictionary:(NSDictionary*)dictionary
{
    self = [super init];
    if (self) {
        _comment = [NSArray array];
        _model = [[SEGSharedNeihborhoodInfoModel alloc] init];
        _user = [[SEGSharedCommonUserModel alloc] init];
        _more = @"";
        [self setValuesForKeysWithDictionary:dictionary];
        //设置layout
        [self saveLayout];
    }
    return self;
}

- (void)setValue:(id)value forKey:(NSString*)key
{
    if ([key isEqualToString:@"user"]) {
        if ([value isKindOfClass:[NSDictionary class]]) {
            _user = [SEGSharedCommonUserModel initWithDictionary:value];
        }
        return;
    }
    
    if ([key isEqualToString:@"comment"]) {
        if ([value isKindOfClass:[NSArray class]]) {
            NSMutableArray* _arrM = [NSMutableArray array];
            for (NSDictionary* dict in value) {
                SEGSharedCommentChildCommentModel* model = [SEGSharedCommentChildCommentModel initWithDictionary:dict];
                [_arrM addObject:model];
            }
            _comment = _arrM.copy;
        }
        return;
    }
    
    if ([key isEqualToString:@"model"]) {
        if ([value isKindOfClass:[NSDictionary class]]) {
            _model = [SEGSharedNeihborhoodInfoModel initWithDictionary:value];
        }
        return;
    }
    
    [super setValue:value forKey:key];
}

+ (instancetype)initWithDictionary:(NSDictionary*)dictionary
{
    return [[self alloc] initWithDictionary:dictionary];
}
- (void)setValue:(id)value forUndefinedKey:(NSString*)key
{}
- (void)setMore:(NSString *)more
{
    _more = [[SEGUtils ValueToString:more]copy];
}


- (void)saveLayout
{
    SEGcustomContentViewModel *data = [[SEGcustomContentViewModel alloc] init];
    data.widgetWidth = LYScreenWidth() - kNeighbourTextLeftPadding - 15;//宽度
    data.like = _model.praiseCount;
    data.comment = @"";//_model.commentCount;
    data.isLiked = [_model.isPraise isEqualToString:@"1"];//是否已点赞
    data.isNeighborListView = YES;

    // 控件内容布局计算
    SEGNeighbourhoodOperationType type = [_model.operationType integerValue];
    switch (type) {
        case SEGNeighbourhoodOperationType_FowardPGC:
        {
            data.contentText = _model.content;
            SEGFocusViewModel *focusDataSource = [[SEGFocusViewModel alloc] init];
            focusDataSource.contentTitle = _model.shareTitle;
            focusDataSource.image = _model.sharePicture;
            focusDataSource.channelName = _model.channelName;
            data.focusDataSource = focusDataSource;
            _layout = [[SEGcustomContentViewLayout alloc] initWithData:data type:SEGEnumcustomContentViewType_FOCUS];
            break;
        }
        case SEGNeighbourhoodOperationType_Market:
        {
            SEGMarketViewModel *marketDataSource = [[SEGMarketViewModel alloc] init];
            marketDataSource.mainTitle = _model.title;
            marketDataSource.contentTitle = _model.content;
            marketDataSource.price = [SEGBasicBussinessEnums getShowPriceWithMarketType:[_model.marketType integerValue] salePrice:_model.salePrice rent:_model.rent unitName:_model.unitName];
            if(_model.picturesArray && _model.picturesArray.count > 0) {
                marketDataSource.image = _model.picturesArray[0];
            }
            data.marketDataSource = marketDataSource;
            
            _layout = [[SEGcustomContentViewLayout alloc] initWithData:data type:SEGEnumcustomContentViewType_MARKET];
            break;
        }
        case SEGNeighbourhoodOperationType_Topic:
        {
            if ([_model.pgcType isEqualToString:@"2"]) {//图文话题
                data.contentText = _model.content;
                SEGTopicPicViewModel *dataSource = [[SEGTopicPicViewModel alloc] init];
                dataSource.topicTitle = _model.shareTitle;
                if (_model.picturesArray.count > 0) {
                    dataSource.picUrlStrArray = _model.picturesArray;
                }
                data.topicPicDataSource = dataSource;
                _layout = [[SEGcustomContentViewLayout alloc] initWithData:data type:SEGEnumcustomContentViewType_TOPIC_PHOTOText];
            }else {
                //文字话题
                SEGTopicTextViewModel *dataSource = [[SEGTopicTextViewModel alloc] init];
                dataSource.content = _model.content;
                dataSource.topicTitle = _model.shareTitle;
                data.topicTextDataSource = dataSource;
                _layout = [[SEGcustomContentViewLayout alloc] initWithData:data type:SEGEnumcustomContentViewType_TOPIC_TEXTONLY];
            }
            break;
        }
        default:
        {
            SEGNeighbourViewModel *NeighbourDataSource = [[SEGNeighbourViewModel alloc] init];
            NeighbourDataSource.type = SEGEnumNeighbourViewType_Neighbour;
            if ([_model.isBroadCast integerValue] > 0) {//求助
                NSMutableString *content = [NSMutableString stringWithString:_model.content];
                if (_model.contacts && _model.contacts.length > 0) {
                    [content appendFormat:@"\n联系人：%@",_model.contacts];
                }
                if (_model.tel && _model.tel.length > 0) {
                    [content appendFormat:@"\n联系方式：%@",_model.tel];
                }
                data.contentText = content;
                data.helpLabelText = _model.labelName;
                //旧数据如有多张图片数据，默认取第一张
                if (_model.picturesArray && _model.picturesArray.count > 1) {
                    NeighbourDataSource.imageContent = [_model.picturesArray subarrayWithRange:NSMakeRange(0, 1)];
                }else {
                    NeighbourDataSource.imageContent = _model.picturesArray;
                }
            }else {
                data.contentText = _model.content;
                NeighbourDataSource.imageContent = _model.picturesArray;
            }
            data.NeighbourDataSource = NeighbourDataSource;
            _layout = [[SEGcustomContentViewLayout alloc] initWithData:data type:SEGEnumcustomContentViewType_NEIGHBOUR_PHOTOText];
            break;
        }
    }

}
@end
